package com.ease.component.util {
   import flash.events.Event;
   import flash.external.ExternalInterface;
   
   import mx.core.FlexGlobals;

   public class FireFoxCmdSupport {
      private var _initialized:Boolean = false;
      
      public function FireFoxCmdSupport() {
         FlexGlobals.topLevelApplication.addEventListener(
            Event.ADDED_TO_STAGE, onAppAddedToStage, false, 0, true);
      }
      
      public static function runCmd(cmd:String, args:Array):void {
         ExternalInterface.call(FUNCTION_RUN_CMD, cmd, args);
      }
      
      private function onAppAddedToStage(e:Event):void {
         if (_initialized) {
            return;
         }
         registerJS();
         _initialized = true;
      }
      
      private function registerJS():void {
         ExternalInterface.call(INSERT_FUNCTION_RUN_CMD);
      }
      
      //firefox only
      //signed.applets.codebase_principal_support:true
      public static var FUNCTION_RUN_CMD:String = "run_cmd";
      public static var INSERT_FUNCTION_RUN_CMD:String = 
         "document.insertScript = function ()" +
         "{" +
                  "if (document." + FUNCTION_RUN_CMD + "==null)" + 
                  "{" + 
                           FUNCTION_RUN_CMD + " = function (cmd, args)" +
                           "{ " +
                                    "netscape.security.PrivilegeManager.enablePrivilege(\"UniversalXPConnect\");" +
                                    "var file = Components.classes[\"@mozilla.org/file/local;1\"].createInstance(Components.interfaces.nsILocalFile);" +
                                    "file.initWithPath(cmd);" +
                                    "var process = Components.classes[\"@mozilla.org/process/util;1\"].createInstance(Components.interfaces.nsIProcess);" +
                                    "process.init(file);" + 
                                    "process.run(false, args, args.length);" +
                           "}" +
                  "}" +
         "}";
   }
}